package com.ylz.sjzx.sio.room.entity;

import com.ylz.sjzx.sio.common.core.enums.Cmd;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author wiqi
 * @version 1.0
 * @date 2021/1/4
 */
@Data
@Builder
@ToString
@NoArgsConstructor
public class LoginDelayItem implements Delayed {
    /**
     * 业务命令
     */
    private Cmd cmd;

    /**
     * 用户的sid
     */
    private String sid;

    /**
     * 超时时间:秒
     */
    private Long delayTime;

    /**
     * 其他参数
     */
    private Object other;

    public LoginDelayItem(Cmd cmd, String sid, Long delay, Object other) {
        this.cmd = cmd;
        this.sid = sid;
        this.delayTime = System.currentTimeMillis() + (delay > 0 ? delay : 0);
        this.other = other;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return this.delayTime - System.currentTimeMillis();
    }

    @Override
    public int compareTo(Delayed o) {
        LoginDelayItem item = (LoginDelayItem) o;
        long diff = this.delayTime - item.delayTime;
        if (diff <= 0) {
            // 改成>=会造成问题
            return -1;
        } else {
            return 1;
        }
    }
}
